package com.xzmzhp.service.finance.impl;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.xzmzhp.constant.RedisKeys;
import com.xzmzhp.dao.finance.FinanceStatisticsMapper;
import com.xzmzhp.pojo.finance.FinanceStatistics;
import com.xzmzhp.service.finance.FinanceStatisticsService;
import com.xzmzhp.utils.RedisUtil;
import com.xzmzhp.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author: 王源(xm)
 * @create: 2022-03-18 11:11
 * @program: ams
 * @description: 财务统计信息逻辑操作类
 */

@Slf4j
@Service
@Transactional(rollbackFor=Exception.class)
public class FinanceStatisticsServiceImpl implements FinanceStatisticsService {

    @Autowired
    private FinanceStatisticsMapper financeStatisticsMapper;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 查询所有财务统计信息
     *
     * @return 财务统计信息
     */
    @Override
    public List<FinanceStatistics> findAll() {
        return financeStatisticsMapper.findAll();
    }


    /**
     * 根据财务统计id查询财务统计信息
     *
     * @param fstatisticsId 财务统计id
     * @return 相关财务统计信息
     */
    @Override
    public FinanceStatistics findFinanceStatisticsByFstatisticsId(Integer fstatisticsId) {
        return financeStatisticsMapper.findFinanceStatisticsByFstatisticsId(fstatisticsId);
    }


    /**
     * 获取余额
     *
     * @return 财务统计信息
     */
    @Override
    public BigDecimal findBalance() {
        //获取缓存中的余额
        BigDecimal bigDecimal = new BigDecimal(redisUtil.get(RedisKeys.BALANCE));
        //若存在则直接返回
        if (StringUtils.isNull(bigDecimal)){
            return new AtomicReference<BigDecimal>(bigDecimal).get();
        }
        //获取当前日期
        Date date = DateUtil.parse(DateUtil.now(), com.xzmzhp.utils.DateUtil.FORMAT_SHORT);
        //查询当前余额
        AtomicReference<BigDecimal> balance = new AtomicReference<>(financeStatisticsMapper.findBalance(date));
        if (StringUtils.isNull(balance.get())){
            balance.set(financeStatisticsMapper.findBalance(DateUtil.offset(date, DateField.DAY_OF_MONTH, -1)));
        }
        //存入redis并返回
        redisUtil.append(RedisKeys.BALANCE,String.valueOf(balance));
        return balance.get();
    }

    /**
     * 根据时间获取信息
     *
     * @param date 开始时间
     * @return 财务统计信息
     */
    @Override
    public FinanceStatistics findFinanceStatisticsByDate(Date date){
        return financeStatisticsMapper.findFinanceStatisticsByDate(date);
    }

    /**
     * 查询一段时间内的财务统计信息
     *
     * @param startDate 开始时间
     * @param endDate   结束时间
     * @return 一段时间内的财务统计信息
     */
    @Override
    public List<FinanceStatistics> countFinanceStatistics(Date startDate, Date endDate) {
        return financeStatisticsMapper.countFinanceStatistics(startDate,endDate);
    }


    /**
     * 根据时间类型查询财务信息
     * @param dateType 时间类型
     * @return 月、年财务信息
     */
    @Override
    public List<FinanceStatistics> findFinanceStatisticsByDateType(Integer dateType) {
        return financeStatisticsMapper.findFinanceStatisticsByDateType(dateType);
    }


    /**
     * 添加财务统计信息
     * @param financeStatistics 财务统计信息
     * @return 是否成功
     */
    @Override
    public Integer addFinanceStatistics(FinanceStatistics financeStatistics) {
        Integer i = financeStatisticsMapper.insert(financeStatistics);
        if (StringUtils.isNotNull(i)){
            redisUtil.append(RedisKeys.BALANCE,String.valueOf(financeStatistics.getBalance()));
        }
        return i;
    }


    /**
     * 修改财务统计信息
     * @param financeStatistics 财务信息
     * @return 是否成功
     */
    @Override
    public Integer editFinanceStatistics(FinanceStatistics financeStatistics) {
        Integer i = financeStatisticsMapper.editFinanceStatistics(financeStatistics);
        if (StringUtils.isNotNull(i)){
            redisUtil.append(RedisKeys.BALANCE,String.valueOf(financeStatistics.getBalance()));
        }
        return i;
    }


    /**
     * 根据时间修改余额信息
     * @param changeAmount 变动金额
     * @param creationTime 创建时间
     * @return 是否成功
     */
    @Override
    public Integer editBalanceByDate(BigDecimal changeAmount, Date creationTime) {
        return financeStatisticsMapper.editBalanceByDate(changeAmount,creationTime);
    }


    /**
     * 删除财务统计信息
     * @param financeStatisticsId 财务统计信息
     * @return 是否成功
     */
    @Override
    public Integer delFinanceStatistics(Integer financeStatisticsId) {
        Integer i = financeStatisticsMapper.deleteById(financeStatisticsId);
        if (StringUtils.isNotNull(i)){
            redisUtil.delete(RedisKeys.BALANCE);
        }
        return i;
    }
}
